home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / browser.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  13KB  |  315 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from gi.repository import Gtk, Pango
  5. from quodlibet import config
  6. from quodlibet import util
  7. from quodlibet import browsers
  8. from quodlibet import app
  9. from quodlibet.qltk.songlist import SongList
  10. from quodlibet.qltk.x import ScrolledWindow
  11. from quodlibet.qltk.window import Window, PersistentWindowMixin
  12. from quodlibet.util.library import background_filter
  13.  
  14. class FilterMenu(object):
  15.     _MENU = "\n        <ui>\n        <menubar name='Menu'>\n        <menu action='Filters'>\n          <menuitem action='FilterGenre' always-show-image='true'/>\n          <menuitem action='FilterArtist' always-show-image='true'/>\n          <menuitem action='FilterAlbum' always-show-image='true'/>\n          <separator/>\n          <menuitem action='RandomGenre' always-show-image='true'/>\n          <menuitem action='RandomArtist' always-show-image='true'/>\n          <menuitem action='RandomAlbum' always-show-image='true'/>\n          <separator/>\n          <menuitem action='All' always-show-image='true'/>\n          <menuitem action='PlayedRecently' always-show-image='true'/>\n          <menuitem action='AddedRecently' always-show-image='true'/>\n          <menuitem action='TopRated' always-show-image='true'/>\n        </menu>\n        </menubar>\n        </ui>"
  16.     
  17.     def __init__(self, library, player, ui = None):
  18.         self._browser = None
  19.         self._library = library
  20.         self._player = player
  21.         ag = Gtk.ActionGroup.new('QuodLibetFilterActions')
  22.         ag.add_actions([
  23.             ('Filters', None, _('_Filters')),
  24.             ('PlayedRecently', Gtk.STOCK_FIND, _('Recently _Played'), '', None, self._FilterMenu__filter_menu_actions),
  25.             ('AddedRecently', Gtk.STOCK_FIND, _('Recently _Added'), '', None, self._FilterMenu__filter_menu_actions),
  26.             ('TopRated', Gtk.STOCK_FIND, _('_Top 40'), '', None, self._FilterMenu__filter_menu_actions),
  27.             ('All', Gtk.STOCK_FIND, _('All _Songs'), '', None, self._FilterMenu__filter_menu_actions)])
  28.         for tag_, lab in [
  29.             ('genre', _('Filter on _Genre')),
  30.             ('artist', _('Filter on _Artist')),
  31.             ('album', _('Filter on Al_bum'))]:
  32.             act = Gtk.Action.new('Filter%s' % util.capitalize(tag_), lab, None, Gtk.STOCK_INDEX)
  33.             act.connect('activate', self._FilterMenu__filter_on, tag_, None, player)
  34.             ag.add_action_with_accel(act, None)
  35.         
  36.         for tag_, accel, label in [
  37.             ('genre', 'G', _('Random _Genre')),
  38.             ('artist', 'T', _('Random _Artist')),
  39.             ('album', 'M', _('Random Al_bum'))]:
  40.             act = Gtk.Action.new('Random%s' % util.capitalize(tag_), label, None, Gtk.STOCK_DIALOG_QUESTION)
  41.             act.connect('activate', self._FilterMenu__random, tag_)
  42.             ag.add_action_with_accel(act, '<control>' + accel)
  43.         
  44.         if not ui:
  45.             pass
  46.         ui = Gtk.UIManager()
  47.         ui.insert_action_group(ag, -1)
  48.         ui.add_ui_from_string(self._MENU)
  49.         self._ui = ui
  50.         ui.get_widget('/Menu/Filters/TopRated').set_tooltip_text(_("The 40 songs you've played most (more than 40 may be chosen if there are ties)"))
  51.         menu_item = ui.get_widget('/Menu/Filters')
  52.         if isinstance(menu_item, Gtk.ImageMenuItem):
  53.             menu_item.set_image(None)
  54.         self._player_id = player.connect('song-started', self._on_song_started)
  55.         self.set_song(player.song)
  56.         self._hide_menus()
  57.  
  58.     
  59.     def destroy(self):
  60.         if self._player:
  61.             self._player.disconnect(self._player_id)
  62.         self._player = None
  63.         self._browser = None
  64.         self._library = None
  65.  
  66.     
  67.     def _on_song_started(self, player, song):
  68.         self.set_song(song)
  69.  
  70.     
  71.     def __random(self, item, key):
  72.         self._browser.filter_random(key)
  73.  
  74.     
  75.     def __filter_on(self, action, header, songs, player):
  76.         if songs is None:
  77.             if player.song:
  78.                 songs = [
  79.                     player.song]
  80.             else:
  81.                 return None
  82.         self._browser.filter_on(songs, header)
  83.  
  84.     
  85.     def __filter_menu_actions(self, menuitem):
  86.         name = menuitem.get_name()
  87.         if name == 'PlayedRecently':
  88.             self._make_query('#(lastplayed < 7 days ago)')
  89.         elif name == 'AddedRecently':
  90.             self._make_query('#(added < 7 days ago)')
  91.         elif name == 'TopRated':
  92.             bg = background_filter()
  93.             if not bg or filter(bg, self._library):
  94.                 pass
  95.             songs = self._library
  96.             songs = [ song.get('~#playcount', 0) for song in songs ]
  97.             if len(songs) == 0:
  98.                 return None
  99.             None.sort()
  100.             if len(songs) < 40:
  101.                 self._make_query('#(playcount > %d)' % (songs[0] - 1))
  102.             else:
  103.                 self._make_query('#(playcount > %d)' % (songs[-40] - 1))
  104.         elif name == 'All':
  105.             self._browser.unfilter()
  106.  
  107.     
  108.     def _make_query(self, query):
  109.         if self._browser.can_filter_text():
  110.             self._browser.filter_text(query.encode('utf-8'))
  111.             self._browser.activate()
  112.  
  113.     
  114.     def _hide_menus(self):
  115.         menus = {
  116.             'genre': [
  117.                 '/Menu/Filters/FilterGenre',
  118.                 '/Menu/Filters/RandomGenre'],
  119.             'artist': [
  120.                 '/Menu/Filters/FilterArtist',
  121.                 '/Menu/Filters/RandomArtist'],
  122.             'album': [
  123.                 '/Menu/Filters/FilterAlbum',
  124.                 '/Menu/Filters/RandomAlbum'],
  125.             None: [
  126.                 '/Menu/Filters/PlayedRecently',
  127.                 '/Menu/Filters/AddedRecently',
  128.                 '/Menu/Filters/TopRated',
  129.                 '/Menu/Filters/All'] }
  130.         for key, widgets in menus.items():
  131.             if self._browser:
  132.                 can_filter = self._browser.can_filter(key)
  133.             else:
  134.                 can_filter = False
  135.             for widget in widgets:
  136.                 self._ui.get_widget(widget).set_property('visible', can_filter)
  137.             
  138.         
  139.  
  140.     
  141.     def set_browser(self, browser):
  142.         self._browser = browser
  143.         self._hide_menus()
  144.  
  145.     
  146.     def set_song(self, song):
  147.         for wid in [
  148.             'FilterAlbum',
  149.             'FilterArtist',
  150.             'FilterGenre']:
  151.             self._ui.get_widget('/Menu/Filters/' + wid).set_sensitive(bool(song))
  152.         
  153.         if song:
  154.             for h in [
  155.                 'genre',
  156.                 'artist',
  157.                 'album']:
  158.                 self._ui.get_widget('/Menu/Filters/Filter%s' % h.capitalize()).set_sensitive(h in song)
  159.             
  160.  
  161.     
  162.     def get_widget(self):
  163.         return self._ui.get_widget('/Menu')
  164.  
  165.     
  166.     def get_accel_group(self):
  167.         return self._ui.get_accel_group()
  168.  
  169.  
  170.  
  171. class LibraryBrowser(Window, util.InstanceTracker, PersistentWindowMixin):
  172.     
  173.     def open(cls, Kind, library, player):
  174.         '''Creates and shows a new browser instance'''
  175.         browser = cls(Kind, library, player)
  176.         browser.show()
  177.         return browser
  178.  
  179.     open = classmethod(open)
  180.     
  181.     def save(cls):
  182.         '''See which browser windows are open and save their names
  183.         so we can restore them on start.
  184.         '''
  185.         names = []
  186.         for browser in cls.instances():
  187.             names.append(browser.name)
  188.         
  189.         config.set('memory', 'open_browsers', '\n'.join(names))
  190.  
  191.     save = classmethod(save)
  192.     
  193.     def restore(cls, library, player):
  194.         '''restore saved browser windows'''
  195.         value = config.get('memory', 'open_browsers', '')
  196.         for name in value.split():
  197.             kind = browsers.get(name)
  198.             browser = cls(kind, library, player)
  199.             browser.show_maybe()
  200.         
  201.  
  202.     restore = classmethod(restore)
  203.     
  204.     def __init__(self, Kind, library, player):
  205.         super(LibraryBrowser, self).__init__(dialog = False)
  206.         self._register_instance()
  207.         self.name = Kind.__name__
  208.         self.set_default_size(600, 400)
  209.         self.enable_window_tracking('browser_' + self.name)
  210.         self.set_title(Kind.name + ' - Quod Libet')
  211.         self.add(Gtk.VBox())
  212.         view = SongList(library, update = True)
  213.         view.info.connect('changed', self._LibraryBrowser__set_time)
  214.         self.songlist = view
  215.         sw = ScrolledWindow()
  216.         sw.set_shadow_type(Gtk.ShadowType.IN)
  217.         sw.add(view)
  218.         sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  219.         self.browser = browser = Kind(library)
  220.         if browser.reordered:
  221.             view.enable_drop()
  222.         elif browser.dropped:
  223.             view.enable_drop(False)
  224.         if browser.accelerators:
  225.             self.add_accel_group(browser.accelerators)
  226.         self._LibraryBrowser__container = browser.pack(sw)
  227.         self.get_child().pack_start(self._LibraryBrowser__container, True, True, 0)
  228.         main = self.get_child()
  229.         bottom = Gtk.HBox()
  230.         main.pack_end(bottom, False, True, 0)
  231.         self._filter_menu = filter_menu = FilterMenu(library, player)
  232.         filter_menu.set_browser(self.browser)
  233.         self.add_accel_group(filter_menu.get_accel_group())
  234.         bottom.pack_start(filter_menu.get_widget(), False, True, 0)
  235.         filter_menu.get_widget().show()
  236.         self._LibraryBrowser__statusbar = Gtk.Label()
  237.         self._LibraryBrowser__statusbar.set_text(_('No time information'))
  238.         self._LibraryBrowser__statusbar.set_alignment(1, 0.5)
  239.         self._LibraryBrowser__statusbar.set_padding(6, 3)
  240.         self._LibraryBrowser__statusbar.set_ellipsize(Pango.EllipsizeMode.START)
  241.         bottom.pack_end(self._LibraryBrowser__statusbar, True, True, 0)
  242.         self._LibraryBrowser__statusbar.show()
  243.         bottom.show()
  244.         browser.connect('songs-selected', self._LibraryBrowser__browser_cb)
  245.         browser.finalize(False)
  246.         view.connect('popup-menu', self._LibraryBrowser__menu, library)
  247.         view.connect('drag-data-received', self._LibraryBrowser__drag_data_recv)
  248.         view.connect('row-activated', self._LibraryBrowser__enqueue, player)
  249.         if browser.headers is not None:
  250.             view.connect('columns-changed', self._LibraryBrowser__cols_changed, browser)
  251.             self._LibraryBrowser__cols_changed(view, browser)
  252.         sw.show_all()
  253.         for c in self.get_child().get_children():
  254.             c.show()
  255.         
  256.         self.get_child().show()
  257.         self.connect('destroy', self._on_destroy)
  258.  
  259.     
  260.     def _on_destroy(self, *args):
  261.         self._filter_menu.destroy()
  262.  
  263.     
  264.     def __browser_cb(self, browser, songs, sorted):
  265.         if browser.background:
  266.             bg = background_filter()
  267.             if bg:
  268.                 songs = filter(bg, songs)
  269.             
  270.         self.songlist.set_songs(songs, sorted)
  271.  
  272.     
  273.     def __enqueue(self, view, path, column, player):
  274.         app.window.playlist.enqueue([
  275.             view.get_model()[path][0]])
  276.         if player.song is None:
  277.             player.next()
  278.  
  279.     
  280.     def __drag_data_recv(self, view, *args):
  281.         if callable(self.browser.reordered):
  282.             self.browser.reordered(view)
  283.         view.clear_sort()
  284.  
  285.     
  286.     def __cols_changed(self, view, browser):
  287.         for header in view.get_columns():
  288.             tag = header.header_name
  289.             for t in util.tagsplit(tag):
  290.                 if t in browser.headers:
  291.                     header.set_visible(True)
  292.                     break
  293.                     continue
  294.                 header.set_visible(False)
  295.  
  296.     
  297.     def __menu(self, view, library):
  298.         (path, col) = view.get_cursor()
  299.         header = col.header_name
  300.         menu = view.Menu(header, self.browser, library)
  301.         if menu is not None:
  302.             view.popup_menu(menu, 0, Gtk.get_current_event_time())
  303.         return True
  304.  
  305.     
  306.     def __set_time(self, info, songs):
  307.         i = len(songs)
  308.         length = sum((lambda .0: pass)(songs))
  309.         t = self.browser.statusbar(i) % {
  310.             'count': i,
  311.             'time': util.format_time_long(length) }
  312.         self._LibraryBrowser__statusbar.set_text(t)
  313.  
  314.  
  315.